package com.example.rectanglesmarktwo;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.google.gson.Gson;

import android.opengl.GLSurfaceView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RectanglesTwo extends Activity {

	private GLSurfaceView mGLView;


	TextView lbl1 = null;
    String serverIP = null;
    EditText enterIP = null;
    Button b1 = null;
    Handler mHandler = null;
	
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    mHandler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	            String text = (String)msg.obj;
	            
	            Log.v("NITESH",text);
	            
	            lbl1.setText(text);
	            
	            //call setText here
	        }
	};
	    
	    
	    mGLView = new MyGLSurfaceView(this);
	    

	    
	    
	    /*
	    
	    b1.setOnClickListener(new View.OnClickListener() {
			
			@SuppressWarnings("unchecked")
			@Override
			public void onClick(View v) {
				
				serverIP = enterIP.getText().toString();
				
				//new network2().execute();
				lbl1.setText("network button clicked");
				//lbl1.setText("waste");
				new network4().execute();
				//lbl1.setText(enterIP.getText());
				
			}
		});//B1 LISTENER END
	    
	    
	    
	    */
	    
	    
	    
	    setContentView(mGLView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    getMenuInflater().inflate(R.menu.activity_rectangles_two, menu);
	    return true;
	}
	
class network4 extends AsyncTask<Object, Integer, Object>{
    	
    	public Socket socket = null;
    	public  ObjectOutputStream objectOutputStream = null;
    	public  ObjectInputStream objectInputStream = null;
    	
    	//String serverIP = "192.168.0.101";
    	int port = 4444;
    	
    	Gson gson = new Gson();	
    	

	public network4(){}
    		
    		
	protected Object doInBackground(Object... params) {
		
		/**
		 *UI calls will NOT work within try/catch
		 *RESTUCTURE THIS
		 */
		
		//lbl1.setText("network 4 successful");
		
		
		try {
			
					
			msg msgMessage = new msg("android device connected");
			String json = gson.toJson(msgMessage);
			
			
			
			
			String msgSend = "android connected";
			
			if(socket == null) {
				socket = new Socket(serverIP, 4444);
				//socket = new Socket("192.168.0.101", 4444);
				objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
				objectInputStream = new ObjectInputStream(socket.getInputStream());
				Log.v("NITESH", "connection established");
				
				
				
			}
			objectOutputStream.writeObject(json);
			//objectOutputStream.writeObject(msgSend);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		while (objectInputStream != null) {
			
			msg message = null;
        	//msg sendMsg = new msg("hello client");
        	
			String str = null;
        	
        	try {
        		
        		
        		//lbl1.setText("network 4 successful");
        		
        		str = (String) objectInputStream.readObject();
        		
        		Message msg = new Message();
        		
        		msg.obj = str;
        		mHandler.sendMessage(msg);
        		
        		
        		Log.v("NITESH", str + "network 4 call");
        		
        		
        	}
        	
        	catch(Exception e){
        		
        		
        	}
			
			
			
		}
		
//		try {
//			socket.close();
//			objectOutputStream.close();
//			objectInputStream.close();
//			socket = null;
//			objectInputStream = null;
//			objectOutputStream = null;
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		// TODO Auto-generated method stub
		return null;
	}

	protected void onProgressUpdate(Integer... progress) {
        
    }

    protected void onPostExecute(Long result) {
        
    }

    
    }
    
    
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    
		Log.e("MENU",item.toString());
		
		if(item.toString().equalsIgnoreCase("Network")){
			
			setContentView(R.layout.network);
			
			b1 = (Button)findViewById(R.id.button1);
	        lbl1 = (TextView)findViewById(R.id.textView1);
	        enterIP = (EditText)findViewById(R.id.editText1);
			
		    b1.setOnClickListener(new View.OnClickListener() {
				
				@SuppressWarnings("unchecked")
				@Override
				public void onClick(View v) {
					
					serverIP = enterIP.getText().toString();
					
					//new network2().execute();
					lbl1.setText("network button clicked");
					//lbl1.setText("waste");
					new network4().execute();
					//lbl1.setText(enterIP.getText());
					
				}
			});//B1 LISTENER END
		
		}
		
		
		if(item.toString().equalsIgnoreCase("settings")){
			
			setContentView(mGLView);
			
			//setContentView(R.layout.activity_rectangles_two);
							}
		
		return true;
	    
	
	

	
	
	
	}}





